<style type="text/css">
.small-title{font-weight:700;font-size:14px}
.select2-container {
z-index: 20000000000; }
.layui-table-cell {
      height: auto !important;
  }
</style>
  <script type="text/html" template>
  <label style="line-height: 40px;">正在修改的网站：{{ d.params.site_ids }}</label>
  </script>
<div class="layui-collapse" lay-filter="component-panel" lay-accordion>
  <div class="layui-colla-item admin-hide">
    <h2 class="layui-colla-title">基本设置</h2>
    <div class="layui-colla-content">
          <div class="layui-row layui-col-space10">
            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              <input id="site-group-checked" type="checkbox" lay-filter="active" title="分组设置" lay-skin="primary">
            </div>
            <div class="layui-col-md10 layui-form layui-hide">
                <select lay-ignore class="site-group-select-update" multiple="multiple" style="width: 300px" name="update_site_group"></select>
            </div>
          </div> 
          <div class="layui-row layui-col-space10 update_http_listen">

            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              <input id="user-package-checked" type="checkbox" lay-filter="active" title="套餐设置" lay-skin="primary">
            </div> 
            <div class="layui-col-md2 layui-hide layui-form">
                <select disabled lay-search lay-filter="update_user_package" id="update_user_package" name="update_user_package" lay-verify="required"></select>
            </div>
          </div> 
          <div class="layui-row layui-col-space10 ">
            <div class="layui-col-md2" style="padding-top:10px"></div> <div class="layui-col-md1"><button id="update_base_btn" type="button" class="layui-btn">修改</button></div>
          </div>

    </div>
  </div>

  <div class="layui-colla-item user-hide">
    <h2 class="layui-colla-title">迁移到其它用户</h2>
    <div class="layui-colla-content">
          <div class="layui-row layui-col-space10">
            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              新用户
            </div>
            <div class="layui-col-md4 layui-form">
              <select  lay-search lay-filter="new-user" id="new-user" name="new-user" lay-verify="required">
                <option value="">请选择用户 (可搜索)</option>
              </select>
            </div>
          </div> 
          <div class="layui-row layui-col-space10 update_http_listen">

            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              用户套餐
            </div> 
            <div class="layui-col-md4 layui-form">
                <select  lay-search lay-filter="update_new_user_package" id="update_new_user_package" name="update_new_user_package" lay-verify="required"></select>
            </div>
          </div> 
          <div class="layui-row layui-col-space10 ">
            <div class="layui-col-md2" style="padding-top:10px"></div> <div class="layui-col-md1"><button id="update_site_uid_btn" type="button" class="layui-btn">修改</button></div>
          </div>

    </div>
  </div>

  <div class="layui-colla-item">
    <h2 class="layui-colla-title">HTTP设置</h2>
    <div class="layui-colla-content">
          <div class="layui-row layui-col-space10">
            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              <input type="checkbox" lay-filter="active" title="状态设置" lay-skin="primary">
            </div>
            <div class="layui-col-md10 layui-form layui-hide"><input type="checkbox" disabled name="update_http_on" checked lay-skin="switch" lay-text="开启|关闭" lay-filter="update_http_on">
            </div>
          </div> 
          <div class="layui-row layui-col-space10 update_http_listen">

            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              <input id="http_listen_port_check" type="checkbox" lay-filter="active" title="监听端口" lay-skin="primary">
            </div> 
            <div class="layui-col-md2 layui-hide"><input type="text" value="" name="update_http_listen_port"  placeholder="请输入端口" autocomplete="off" class="layui-input" disabled></div>
          </div> 

          <div class="layui-row layui-col-space10 ">
            <div class="layui-col-md2" style="padding-top:10px"></div> <div class="layui-col-md1"><button id="update_http_btn" type="button" class="layui-btn">修改</button></div>
          </div>

    </div>
  </div>
  <div class="layui-colla-item">
    <h2 class="layui-colla-title">HTTPS设置</h2>
    <div class="layui-colla-content">
        <div class="layui-row layui-col-space10 ">
            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              <input type="checkbox" lay-filter="active" title="状态设置" lay-skin="primary">
            </div>
          <div class="layui-col-md10 layui-form layui-hide"><input disabled type="checkbox" lay-filter="update_https_on" name="update_https_on" lay-skin="switch" checked lay-text="开启|关闭">
          </div>
        </div> 
        <div class="layui-row layui-col-space10 update_https_listen admin-hide">
            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              <input id="https_cert_check" type="checkbox" lay-filter="active" title="证书" lay-skin="primary">
            </div>          
           <div class="layui-col-md3 layui-form layui-hide">
                <select disabled lay-search lay-filter="update_cert" id="update_cert" name="update_cert" lay-verify="required">
                </select>
          </div>
        </div>           
        <div class="layui-row layui-col-space10 update_https_listen">
            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              <input id="https_listen_port_check" type="checkbox" lay-filter="active" title="监听端口" lay-skin="primary">
            </div>
          <div class="layui-col-md2 layui-hide"><input disabled type="text" name="update_https_listen_port"  placeholder="请输入端口"  value="" autocomplete="off" class="layui-input"></div>
        </div>            

        <div class="layui-row layui-col-space10 update_https_listen">
            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              <input id="https_hsts_check" type="checkbox" lay-filter="active" title="HSTS" lay-skin="primary">
            </div>
          <div class="layui-col-md10 layui-form layui-hide">
            <input disabled type="radio" lay-filter="update_hsts" name="update_hsts" value=1 title="开启">
            <input disabled type="radio" lay-filter="update_hsts" name="update_hsts" value=0 title="关闭" >
          </div>
        </div>

        <div class="layui-row layui-col-space10 update_https_listen">
          <div class="layui-col-md2 layui-form" style="padding-top:10px">
            <input id="https_http2_check" type="checkbox" lay-filter="active" title="HTTP2" lay-skin="primary">
          </div>
          <div class="layui-col-md10 layui-form layui-hide">
            <input disabled type="radio" lay-filter="update_http2" name="update_http2" value=1 title="开启">
            <input disabled type="radio" lay-filter="update_http2" name="update_http2" value=0 title="关闭" >
          </div>
        </div>

        <div class="layui-row layui-col-space10 update_https_listen">
          <div class="layui-col-md2 layui-form" style="padding-top:10px">
            <input id="https_ssl_config_check" type="checkbox" lay-filter="active" title="SSL配置" lay-skin="primary">
          </div>
          <div class="layui-col-md10 layui-form layui-hide">
                <input type="radio" lay-filter="ssl_mode" name="ssl_mode" value="old" checked title="兼容旧浏览器(安全性降低)">
                <input type="radio" lay-filter="ssl_mode" name="ssl_mode" value="intermediate" title="兼容大部分浏览器(更安全)">
                <input type="radio" lay-filter="ssl_mode" name="ssl_mode" value="custom" title="自定义">
          </div>
        </div>

        <div class="layui-row layui-col-space10  ssl-config layui-hide">
          <div class="layui-col-md2" style="padding-top:10px"></div>
          <div class="layui-col-md10 layui-form">
            <div class="layui-row layui-col-space10 ">
              <div class="layui-col-md4" style="padding-top:10px">ssl_protocols：</div> <div class="layui-col-md6"><input type="text" name="https_listen_ssl_protocols" value="TLSv1 TLSv1.1 TLSv1.2 TLSv1.3"   autocomplete="off" class="layui-input"></div>
            </div>     
            <div class="layui-row layui-col-space10">
              <div class="layui-col-md4" style="padding-top:10px">ssl_ciphers：</div> <div class="layui-col-md6"><input type="text" name="https_listen_ssl_ciphers"  value="ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA"  autocomplete="off" class="layui-input"></div>
            </div>      
            <div class="layui-row layui-col-space10">
              <div class="layui-col-md4" style="padding-top:10px">ssl_prefer_server_ciphers：</div> <div class="layui-col-md6"><input type="text" name="https_listen_ssl_prefer_server_ciphers" value="on"  autocomplete="off" class="layui-input"></div>
            </div>                 
          </div>
        </div>

        <div class="layui-row layui-col-space10 update_https_listen">
          <div class="layui-col-md2 layui-form" style="padding-top:10px">
            <input id="https_force_ssl_check" type="checkbox" lay-filter="active" title="强制HTTPS" lay-skin="primary">
          </div>
          <div class="layui-col-md10 layui-form layui-hide">
            <div class="layui-input-inline">
              <input disabled type="radio" lay-filter="update_force_ssl_enable" name="update_force_ssl_enable" value=1 title="开启">
              <input disabled type="radio" lay-filter="update_force_ssl_enable" name="update_force_ssl_enable" value=0 title="关闭" >
            </div>
            <div class="layui-input-inline">
              <input disabled type="text" name="update_force_ssl_port"  placeholder="请输入端口"  value="443" autocomplete="off" class="layui-input">
            </div>
          </div>
        </div>        

        <div class="layui-row layui-col-space10 update_https_listen">
            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              <input id="https_ocsp_stapling_check" type="checkbox" lay-filter="active" title="OCSP stapling" lay-skin="primary">
            </div>
          <div class="layui-col-md10 layui-form layui-hide">
            <input disabled type="radio" lay-filter="update_ocsp_stapling" name="update_ocsp_stapling" value=1 title="开启">
            <input disabled type="radio" lay-filter="update_ocsp_stapling" name="update_ocsp_stapling" value=0 title="关闭" >
          </div>
        </div>

        <div class="layui-row layui-col-space10 ">
          <div class="layui-col-md2" style="padding-top:10px"></div> <div class="layui-col-md1"><button id="update_https_btn" type="button" class="layui-btn">修改</button></div>
        </div>

    </div>
  </div>
  <div class="layui-colla-item">
    <h2 class="layui-colla-title">源站设置</h2>
    <div class="layui-colla-content">
        <div class="layui-row layui-col-space10">
            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              <input type="checkbox" id="update_backend_protocol_on" lay-filter="active" title="回源协议" lay-skin="primary">
            </div>
          <div class="layui-col-md3 layui-form layui-hide">
                <select disabled lay-search lay-filter="update_backend_protocol" id="update_backend_protocol" name="update_backend_protocol" lay-verify="required">
                  <option value="">请选择</option>
                  <option value="http">HTTP</option>
                  <option value="https">HTTPS</option>
                  <option value="follow">跟随</option>
                </select>
          </div>
        </div>

        <div class="layui-row layui-col-space10">
            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              <input id="update_backend_http_port_on" type="checkbox" lay-filter="active" title="HTTP回源端口" lay-skin="primary">
            </div>
          <div class="layui-col-md2 layui-hide"><input disabled type="text" name="update_backend_http_port"  placeholder="请输入端口"  value="" autocomplete="off" class="layui-input"></div>
        </div> 

        <div class="layui-row layui-col-space10">
            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              <input id="update_backend_https_port_on" type="checkbox" lay-filter="active" title="HTTPS回源端口" lay-skin="primary">
            </div>
          <div class="layui-col-md2 layui-hide"><input disabled type="text" name="update_backend_https_port"  placeholder="请输入端口"  value="" autocomplete="off" class="layui-input"></div>
        </div>

        <div class="layui-row layui-col-space10">
            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              <input type="checkbox" id="update_backend_on" lay-filter="active" title="源IP列表" lay-skin="primary">
            </div> 
          <div class="layui-col-md8 layui-form layui-hide">
              <textarea disabled name="update_backend_ip" required lay-verify="required" placeholder="格式为&#13;&#10;192.168.0.1&#13;&#10;192.168.0.2 backup" class="layui-textarea"></textarea>            
          </div>
        </div>

        <div class="layui-row layui-col-space10">
            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              <input id="update_proxy_timeout_on" type="checkbox" lay-filter="active" title="回源超时" lay-skin="primary">
            </div>
          <div class="layui-col-md2 layui-hide"><input disabled type="text" name="update_proxy_timeout"  placeholder="最大900"  value="" autocomplete="off" class="layui-input"></div>
        </div>

        <div class="layui-row layui-col-space10">
            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              <input type="checkbox" id="update_balance_on" lay-filter="active" title="负载方式" lay-skin="primary">
            </div>
          <div class="layui-col-md3 layui-form layui-hide">
                <select disabled lay-search lay-filter="update_balance_way" id="update_balance_way" name="update_balance_way" lay-verify="required">
                  <option value="">请选择</option>
                  <option value="ip_hash">ip hash</option>
                  <option value="rr">轮循</option>
                  <option value="url_hash">url hash</option>
                  <option value="least_conn">最少连接数</option>
                  <option value="random">随机</option>
                </select>
          </div>
        </div>

        <div class="layui-row layui-col-space10 update_https_listen">
            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              <input id="update_backend_port_mapping_on" type="checkbox" lay-filter="active" title="回源端口映射" lay-skin="primary">
            </div>
          <div class="layui-col-md10 layui-form layui-hide">
            <input disabled type="radio" lay-filter="update_backend_port_mapping" name="update_backend_port_mapping" value=1 title="开启">
            <input disabled type="radio" lay-filter="update_backend_port_mapping" name="update_backend_port_mapping" value=0 title="关闭" >
          </div>
        </div>

        <div class="layui-row layui-col-space10 ">
          <div class="layui-col-md2" style="padding-top:10px"></div> <div class="layui-col-md1"><button id="update_backend_btn" type="button" class="layui-btn">修改</button></div>
        </div>

    </div>
  </div>

  <div class="layui-colla-item">
    <h2 class="layui-colla-title">缓存配置</h2>
    <div class="layui-colla-content">

        <div class="layui-row layui-col-space10">
          <div class="layui-col-md12">
          <table id="cache" lay-filter="cache"></table>
            <script type="text/html" id="cache-toolbar">
                <div class="layui-row layui-col-space10">
                  <div class="layui-col-md12">
                      <button class="layui-btn  layui-btn-xs" lay-event="add">新增</button>
                      <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</button>
                  </div>  
                </div>
            </script>
            <script type="text/html" id="cache-bar">
              <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            </script>
        </div>
       </div>

        <div class="layui-row layui-col-space10 ">
          <div class="layui-col-md2" style="padding-top:10px"></div> <div class="layui-col-md1"><button id="update_proxy_cache_btn" type="button" class="layui-btn">修改</button></div>
        </div>

    </div>
  </div>

  <div class="layui-colla-item">
    <h2 class="layui-colla-title">安全设置</h2>
    <div class="layui-colla-content">
        <div class="layui-row layui-col-space10">
          <div class="layui-col-md2 layui-form" style="padding-top:10px">
            <input id="cc_default_rule_check" type="checkbox" lay-filter="active" title="默认CC规则" lay-skin="primary">
          </div>
          <div class="layui-col-md3 layui-form layui-hide">
              <select disabled lay-search lay-filter="update_cc_default_rule" id="update_cc_default_rule" name="update_cc_default_rule" lay-verify="required">
                <option value="">请选择</option>
                <optgroup class="update_internal_cc_rule" label="系统内置规则">

                </optgroup>
                <optgroup class="update_custom_cc_rule" label="自定义规则">

                </optgroup>

              </select>
          </div>
        </div>

        <div class="layui-row layui-col-space10">
          <div class="layui-col-md2 layui-form" style="padding-top:10px">
            <input id="auto_switch_check" type="checkbox" lay-filter="active" title="自动防护" lay-skin="primary">
          </div>
          <div class="layui-col-md10 layui-form layui-hide">
            <div class="layui-form-item">
              <div style="margin-bottom: 12px;" class="layui-inline">
                <input type="checkbox" lay-filter="update_cc_switch_on" name="update_cc_switch_on" lay-skin="switch" lay-text="开启|关闭">
              </div>
            
              <div class="layui-inline update_cc_switch_on">
                <div class="layui-form-mid layui-word-aux">当网站每秒请求数大于</div>
                <div class="layui-input-inline update-custom-qps">
                  <input type="text" name="update-switch-qps" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">时，防护等级提升到</div>

                <div class="layui-input-inline">
                  <select lay-search  id="update_cc_switch_rule" name="update_cc_switch_rule" lay-verify="required">
                    <option value="">请选择</option>
                    <optgroup class="update_internal_cc_rule" label="系统内置规则">

                    </optgroup>
                    <optgroup class="update_custom_cc_rule" label="自定义规则">

                    </optgroup>

                  </select>
                </div>                     

              </div>



            </div>
          </div>
        </div>

        <div class="layui-row layui-col-space10">
          <div class="layui-col-md2 layui-form" style="padding-top:10px">
            <input type="checkbox" id="update_api_protect_on" lay-filter="active" title="自定义规则" lay-skin="primary">
          </div>
        <div class="layui-col-md10 layui-form layui-hide">
          <div class="layui-row layui-col-space10">
            <div class="layui-col-md12">
            <table id="api-protect" lay-filter="api-protect"></table>
              <script type="text/html" id="api-protect-toolbar">
                  <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <button class="layui-btn  layui-btn-xs" lay-event="add">新增</button>
                        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</button>
                    </div>  
                  </div>
              </script>

          </div>
         </div>
        </div>
      </div>

        <div class="layui-row layui-col-space10">
          <div class="layui-col-md2 layui-form" style="padding-top:10px">
            <input id="update_block_proxy_on" type="checkbox" lay-filter="active" title="屏蔽透明代理" lay-skin="primary">
          </div>
          <div class="layui-col-md10 layui-form layui-hide">
            <input disabled type="radio" lay-filter="update_block_proxy" name="update_block_proxy" value=1 title="是">
            <input disabled type="radio" lay-filter="update_block_proxy" name="update_block_proxy" value=0 title="否" >
          </div>
        </div>

        <div class="layui-row layui-col-space10">
            <div class="layui-col-md2 layui-form" style="padding-top:10px">
              <input type="checkbox" id="update_block_region_on" lay-filter="active" title="屏蔽区域" lay-skin="primary">
            </div>
          <div class="layui-col-md3 layui-form layui-hide">
                <select disabled lay-search lay-filter="update_block_region" id="update_block_region" name="update_block_region" lay-verify="required">
                  <option value="">无</option>
                  <option value="hk,mo,tw,mn,kp,kr,jp,vn,la,kh,th,mm,my,sg,id,bn,ph,tl,in,bd,bt,np,pk,lk,mv,sa,qa,bh,kw,ae,om,ye,ge,lb,sy,il,ps,jo,iq,ir,af,cy,az,tm,tj,kg,uz,kz,dz,ao,bj,bw,bf,bi,cm,cv,cf,td,km,ci,cd,dj,eg,gq,er,et,ga,gm,gh,gn,gw,ke,ls,lr,ly,mg,mw,ml,mr,mu,ma,mz,na,ne,ng,cg,rw,st,sn,sc,sl,so,za,sd,ss,tz,tg,tn,ug,zm,zw,ag,bs,bb,bz,ca,cr,cu,dm,do,sv,ai,bm,gl,gd,gp,gt,ht,hn,jm,mq,mx,ms,aw,cw,ni,pa,kn,lc,vc,tt,tc,us,mf,pr,bl,sx,ar,bo,br,cl,co,ec,gy,py,pe,sr,uy,ve,al,ad,am,at,by,be,ba,bg,hr,cz,dk,ee,fi,fr,de,gr,hu,is,ie,it,lv,li,lt,lu,mk,mt,md,mc,me,nl,no,pl,pt,ro,ru,sm,rs,sk,si,es,se,ch,tr,ua,uk,va,au,pg,nz,fj,sb,pf,nc,vu,ws,gu,fm,to,ki,as,pw,wf,nr,tv,nu,tk">境外(包括港澳台)</option>
                  <option value="mn,kp,kr,jp,vn,la,kh,th,mm,my,sg,id,bn,ph,tl,in,bd,bt,np,pk,lk,mv,sa,qa,bh,kw,ae,om,ye,ge,lb,sy,il,ps,jo,iq,ir,af,cy,az,tm,tj,kg,uz,kz,dz,ao,bj,bw,bf,bi,cm,cv,cf,td,km,ci,cd,dj,eg,gq,er,et,ga,gm,gh,gn,gw,ke,ls,lr,ly,mg,mw,ml,mr,mu,ma,mz,na,ne,ng,cg,rw,st,sn,sc,sl,so,za,sd,ss,tz,tg,tn,ug,zm,zw,ag,bs,bb,bz,ca,cr,cu,dm,do,sv,ai,bm,gl,gd,gp,gt,ht,hn,jm,mq,mx,ms,aw,cw,ni,pa,kn,lc,vc,tt,tc,us,mf,pr,bl,sx,ar,bo,br,cl,co,ec,gy,py,pe,sr,uy,ve,al,ad,am,at,by,be,ba,bg,hr,cz,dk,ee,fi,fr,de,gr,hu,is,ie,it,lv,li,lt,lu,mk,mt,md,mc,me,nl,no,pl,pt,ro,ru,sm,rs,sk,si,es,se,ch,tr,ua,uk,va,au,pg,nz,fj,sb,pf,nc,vu,ws,gu,fm,to,ki,as,pw,wf,nr,tv,nu,tk">境外(不包括港澳台)</option>
                  <option value="cn,hk,mo,tw">境内(包括港澳台)</option>
                  <option value="cn">境内(不包括港澳台)</option>
                </select>
          </div>
        </div>
     
        <div class="layui-row layui-col-space10">
          <div class="layui-col-md2 layui-form" style="padding-top:10px">
            <input id="update_black_ip_on" type="checkbox" lay-filter="active" title="黑名单" lay-skin="primary">
          </div> 
          <div class="layui-col-md7 layui-form layui-hide">
            <textarea disabled name="update_black_ip" required lay-verify="required" placeholder="请输入" class="layui-textarea"></textarea>

          </div> 
        </div>      
        <div class="layui-row layui-col-space10">
          <div class="layui-col-md2 layui-form" style="padding-top:10px">
            <input id="update_white_ip_on" type="checkbox" lay-filter="active" title="白名单" lay-skin="primary">
          </div> 
          <div class="layui-col-md7 layui-form layui-hide">
            <textarea disabled name="update_white_ip" required lay-verify="required" placeholder="请输入" class="layui-textarea"></textarea>

          </div> 
        </div> 

        <div class="layui-row layui-col-space10 ">
          <div class="layui-col-md2" style="padding-top:10px"></div> <div class="layui-col-md1"><button id="update_cc_btn" type="button" class="layui-btn">修改</button></div>
        </div>

    </div>

  </div>

  <div class="layui-colla-item">
    <h2 class="layui-colla-title">访问控制</h2>
    <div class="layui-colla-content">
      <div class="layui-row layui-col-space10 admin-hide">
          <div class="layui-col-md2 layui-form" style="padding-top:10px">
            <input id="update_acl_on" type="checkbox" lay-filter="active" title="ACL规则" lay-skin="primary">
          </div> 

         <div class="layui-col-md3 layui-form layui-hide">
              <select disabled lay-search lay-filter="update_acl" id="update_acl" name="update_acl" lay-verify="required">
              </select>
        </div>
      </div> 

      <div class="layui-row layui-col-space10">
        <div class="layui-col-md2 layui-form" style="padding-top:10px">
          <input id="update_hotlink_on" type="checkbox" lay-filter="active" title="防盗链" lay-skin="primary">
        </div> 
        <div class="layui-col-md10 layui-form layui-hide">
          <div class="layui-row layui-col-space10 layui-form">
            <div class="layui-col-md2 " style="padding-top:10px">开启</div> 
            <input type="checkbox"  lay-filter="hotlink_enable" name="hotlink_enable" lay-skin="switch" lay-text="是|否">
          </div>

          <div class="layui-row layui-col-space10 ">
            <div class="layui-col-md2" style="padding-top:10px">防盗链范围</div> 
            <div class="layui-col-md10 layui-form">
                  <input type="radio" lay-filter="hotlink_scope_type" name="hotlink_scope_type" value="all" checked title="整站">
                  <input type="radio" lay-filter="hotlink_scope_type"  name="hotlink_scope_type" value="suffix" title="后缀">
                  <input type="radio" lay-filter="hotlink_scope_type"  name="hotlink_scope_type" value="dir" title="目录">
                  <input type="radio" lay-filter="hotlink_scope_type"  name="hotlink_scope_type" value="full_path" title="全路径">
            </div> 
          </div> 

          <div class="layui-row layui-col-space10 hotlink-scope-content layui-hide">
            <div class="layui-col-md2" style="padding-top:10px"></div> 
            <div class="layui-col-md10 layui-form">
              <input type="text" name="hotlink_scope_content"  placeholder="输入内容"  value="" autocomplete="off" class="layui-input"> 

            </div> 
          </div>


          <div class="layui-row layui-col-space10  layui-form">
            <div class="layui-col-md2 " style="padding-top:10px">允许空来源</div> 
            <div style="line-height: 30px;" class="layui-col-md10 layui-form">
              <input type="checkbox"  lay-filter="hotlink_allow_empty" name="hotlink_allow_empty" title="" lay-skin="primary"> 
            </div> 
          </div>   
          <div class="layui-row layui-col-space10 ">
            <div class="layui-col-md2" style="padding-top:10px">允许的域名</div> 
            <div class="layui-col-md10 layui-form">
              <input type="text" name="hotlink_domain"  placeholder="请输入除当前网站域名之外的域名"  value="" autocomplete="off" class="layui-input"> 
            </div> 
          </div>    
          
        </div> 
      </div> 

    <div class="layui-row layui-col-space10 ">
      <div class="layui-col-md2" style="padding-top:10px"></div> <div class="layui-col-md1"><button id="update_acl_btn" type="button" class="layui-btn">修改</button></div>
    </div>

    </div>
  </div>

  <div class="layui-colla-item">
    <h2 class="layui-colla-title">回源设置</h2>
    <div class="layui-colla-content">
      <div class="layui-row layui-col-space10">
        <div class="layui-col-md2 layui-form" style="padding-top:10px">
          <input id="update_backend_host_on" type="checkbox" lay-filter="active" title="回源域名" lay-skin="primary">
        </div> 

        <div class="layui-col-md4 layui-form layui-hide">
          <input disabled type="radio" lay-filter="update_backend_host_type" name="update_backend_host_type" value="$host" title="访问域名" checked>
          <input disabled type="radio" lay-filter="update_backend_host_type" name="update_backend_host_type" value="custom" title="自定义" >
        </div>

        <div class="layui-col-md4 layui-form update_backend_host layui-hide">
          <input disabled type="text" name="update_backend_host"  placeholder="请输入域名"  value="" autocomplete="off" class="layui-input"> 
        </div>

      </div> 

      <div class="layui-row layui-col-space10">
        <div class="layui-col-md2 layui-form" style="padding-top:10px">
          <input id="update_range_on" type="checkbox" lay-filter="active" title="Range回源" lay-skin="primary">
        </div> 
        <div class="layui-col-md2 layui-form layui-hide">
          <input disabled type="checkbox"  lay-filter="update_range" name="update_range" lay-skin="switch" lay-text="开启|关闭">
        </div>
      </div>  

      <div class="layui-row layui-col-space10 ">
        <div class="layui-col-md2" style="padding-top:10px"></div> <div class="layui-col-md1"><button id="update_backend_host_btn" type="button" class="layui-btn">修改</button></div>
      </div>   

    </div>
  </div>

  <div class="layui-colla-item">
    <h2 class="layui-colla-title">高级设置</h2>
    <div class="layui-colla-content">
      <div class="layui-row ">
       <div class="layui-col-md2"> <span class="small-title">压缩设置</span></div>
      </div>
      <div class="layui-row layui-col-space10">
        <div class="layui-col-md2 layui-form" style="padding-top:10px">
          <input id="update_gzip_on" type="checkbox" lay-filter="active" title="Gzip压缩" lay-skin="primary">
        </div> 
        <div class="layui-col-md10 layui-form layui-hide"><input disabled type="checkbox" lay-filter="update_gzip_enable"  name="update_gzip_enable" lay-skin="switch" lay-text="开启|关闭">
        </div>                 
      </div> 
      <div class="layui-row layui-col-space10">
        <div class="layui-col-md2 layui-form" style="padding-top:10px">
          <input id="update_gzip_types_on" type="checkbox" lay-filter="active" title="Gzip类型" lay-skin="primary">
        </div> 
        <div class="layui-col-md7 layui-form layui-hide">
          <input disabled type="text" name="update_gzip_types"  placeholder="请输入内容类型"  value="" autocomplete="off" class="layui-input"> 
         
        </div>                 
      </div>  
      <div class="layui-row" style="padding-top:20px;">
       <div class="layui-col-md2"> <span class="small-title">websocket配置</span></div>
      </div>
      <div class="layui-row layui-col-space10">
        <div class="layui-col-md2 layui-form" style="padding-top:10px">
          <input id="update_websocket_on" type="checkbox" lay-filter="active" title="Websocket" lay-skin="primary">
        </div> 
        <div class="layui-col-md10 layui-form layui-hide " ><input disabled type="checkbox"  lay-filter="update_websocket" name="update_websocket" lay-skin="switch" lay-text="开启|关闭">
        </div>                 
      </div>     
      <div class="layui-row ">
       <div class="layui-col-md2" style="padding-top:20px;"> <span class="small-title">错误页面配置</span></div>
      </div>
      <div class="layui-row layui-col-space10">
        <div class="layui-col-md2 layui-form" style="padding-top:10px">
          <input id="update_404_on" type="checkbox" lay-filter="active" title="404页面" lay-skin="primary">
        </div> 
        <div class="layui-col-md7 layui-form layui-hide">
          <textarea disabled name="update_page_404" lay-filter="update_page_404" required lay-verify="required" placeholder="请输入" class="layui-textarea"></textarea>

        </div> 
      </div>      
      <div class="layui-row layui-col-space10">
        <div class="layui-col-md2 layui-form" style="padding-top:10px">
          <input id="update_50x_on" type="checkbox" lay-filter="active" title="50x页面" lay-skin="primary">
        </div> 
        <div class="layui-col-md7 layui-form layui-hide">
          <textarea disabled name="update_page_50x" lay-filter="update_page_50x" required lay-verify="required" placeholder="请输入" class="layui-textarea"></textarea>

        </div> 
      </div> 

      <div class="layui-row" style="padding-top:20px;">
        <div class="layui-col-md2"> <span class="small-title">请求头响应头</span></div>
      </div>
      <div class="layui-row layui-col-space10">
        <div class="layui-col-md2 layui-form" style="padding-top:10px">
          <input id="update_req_header_on"  type="checkbox" lay-filter="active" title="源站请求头" lay-skin="primary">
        </div> 
        <div class="layui-col-md10 layui-form layui-hide">
          <div class="layui-row layui-col-space10">
            <div class="layui-col-md12">
            <table id="req_header" lay-filter="req_header"></table>
              <script type="text/html" id="req_header-toolbar">
                  <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <button class="layui-btn  layui-btn-xs" lay-event="add">新增</button>
                        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</button>
                    </div>  
                  </div>
              </script>
              <script type="text/html" id="req_header-bar">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
              </script>
          </div>
        </div>
        </div> 
      </div>

      <div class="layui-row layui-col-space10">
        <div class="layui-col-md2 layui-form" style="padding-top:10px">
          <input id="update_resp_header_on"  type="checkbox" lay-filter="active" title="CDN响应头" lay-skin="primary">
        </div> 
        <div class="layui-col-md10 layui-form layui-hide">
          <div class="layui-row layui-col-space10">
            <div class="layui-col-md12">
            <table id="resp_header" lay-filter="resp_header"></table>
              <script type="text/html" id="resp_header-toolbar">
                  <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <button class="layui-btn  layui-btn-xs" lay-event="add">新增</button>
                        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</button>
                    </div>  
                  </div>
              </script>
              <script type="text/html" id="resp_header-bar">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
              </script>
          </div>
        </div>
        </div> 
      </div>

      <div class="layui-row" style="padding-top:20px;">
        <div class="layui-col-md2"> <span class="small-title">URL转向</span></div>
      </div>
      <div class="layui-row layui-col-space10">
        <div class="layui-col-md2 layui-form" style="padding-top:10px">
          <input id="update_url_rewrite_on"  type="checkbox" lay-filter="active" title="URL转向" lay-skin="primary">
        </div> 
        <div class="layui-col-md10 layui-form layui-hide">
          <div class="layui-row layui-col-space10">
            <div class="layui-col-md12">
            <table id="url_rewrite" lay-filter="url_rewrite"></table>
              <script type="text/html" id="url_rewrite-toolbar">
                  <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <button class="layui-btn  layui-btn-xs" lay-event="add">新增</button>
                        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</button>
                    </div>  
                  </div>
              </script>
              <script type="text/html" id="url_rewrite-bar">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
              </script>
          </div>
        </div>
        </div> 
      </div>
      
      <div class="layui-row layui-col-space10 ">
        <div class="layui-col-md2" style="padding-top:10px"></div> <div class="layui-col-md1"><button id="update_advance_btn" type="button" class="layui-btn">修改</button></div>
      </div> 

    </div>
  </div>

</div>

<script>
layui.use(['admin'], function(){
  var $ = layui.$
  ,admin = layui.admin
  ,element = layui.element
  ,form = layui.form
  ,table = layui.table
  ,view = layui.view
  ,router = layui.router();

  form.on('radio(hotlink_scope_type)', function(data){
      if (data.value == "all") {
        $(".hotlink-scope-content").addClass("layui-hide")
      } else {
        if (data.value == "suffix") {
          $("input[name='hotlink_scope_content']").attr("placeholder","请输入后缀，如png|jpg|jpeg|icon")

        } else if (data.value == "dir") {
          $("input[name='hotlink_scope_content']").attr("placeholder","请输入目录，如/image/|/static/|/upload/")

        } else if (data.value == "full_path") {
          $("input[name='hotlink_scope_content']").attr("placeholder","请输入全路径，如/image/1.png|/image/2.png")

        }
        $(".hotlink-scope-content").removeClass("layui-hide")
      }

    }); 

  window.match_item_map = {"ip":"IP地址","host":"域名","req_uri":"请求URI","uri":"请求URI(不带参数)","req_method":"请求方法","user_agent":"浏览器UA","referer":"请求来源","country_iso_code":"国家代码"}
  window.match_op_map = {"=":"等于","!=":"不等于","contain":"包含","!contain":"不包含","prefix":"前缀匹配","suffix":"后缀匹配","regex":"正则匹配"}
  window.filter_type_map = {"req_rate":"请求频率","browser_verify_auto": "无感验证", "delay_jump_filter":"5秒盾","click_filter":"点击验证","slide_filter":"滑动验证","captcha_filter":"验证码","rotate_filter":"旋转图片","302_challenge":"302跳转","url_auth":"URL鉴权"}

  element.render('collapse');
  window.cache = []

  // 初始化cache
  element.on('collapse(component-panel)', function(data){
    if (data.title.text().startsWith("缓存配置")) {
      table.resize("cache")
    }

  });  

  var user_type = layui.data('layuiAdmin')['user_type']
  if (user_type == 1) {
    //请求用户列表
    admin.req({
      url: '/users?limit=0&type=2' //实际使用请改成服务端真实接口
      ,type: "get"
      ,contentType:"application/json"
      ,dataType: "json"
      ,done: function(res){
        var data = res.data
        for (i in data) {
          var id = data[i]['id']
          var name = data[i]['name']
          var phone = data[i]['phone']
          var email = data[i]['email']
          var identify = id
          if (name) {
            identify = name 
          } else if (phone) {
            identify = phone
          } else if (email) {
            identify = email
          }

          $("#new-user").append("<option value='"+data[i]["id"]+"'>"+identify+"</option>");
        }

        form.render("select");
      }
    }); 

    form.on('select(new-user)', function(data){
      var uid = data.value
      //请求套餐数据
      admin.req({
        url: '/user-packages?limit=0&uid='+uid //实际使用请改成服务端真实接口
        ,type: "get"
        ,contentType:"application/json"
        ,dataType: "json"
        ,done: function(res){
          var data = res.data
          $("#update_new_user_package").empty()
          for (i in data) {
            var package_name = data[i]['package_name']
            var user_package_name = data[i]['user_package_name']
            var name = package_name
            if (package_name != user_package_name) {
              name = package_name + "(" + user_package_name + ")"
            }
            $("#update_new_user_package").append("<option value='"+data[i]["id"]+"'>"+name+"</option>");
          }

          form.render("select");
        }
      });
    })    

    $("#update_site_uid_btn").click(function() {
      // 新用户
      var new_uid = $("#new-user").val()

      // 套餐
      var user_package = $("select[name='update_new_user_package']").val()

      if (new_uid == "" || user_package == "") {
        layer.alert("请选择用户或者用户套餐")
        return
      }

      var req_data = []
      for (i in window.site_ids) {
        req_data.push({"id":window.site_ids[i],"new_uid":new_uid,"uid":new_uid,"user_package":user_package})
      }

      update_sites(req_data)      
    })

  } else {
    $(".user-hide").addClass("layui-hide")

  }

  table.render({
    elem: '#cache'
    ,title: 'xx'
    ,toolbar: '#cache-toolbar'
    ,defaultToolbar: []
      ,size: 'sm' //小尺寸的表格

    ,cols: [[ //表头
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'type', title: '类型',templet: function(d) {
        if (d.type == "suffix") {
          return "后缀名"
        } else if (d.type == "dir") {
          return "目录"
        } else {
          return "全路径"
        }
      }}
      ,{field: 'content', title: '内容'}
      ,{field: 'expire', title: '有效期',templet: function(d) {
          return d.expire+d.unit
      }}
      ,{field: 'ignore_arg', title: '忽略参数',templet: function(d) {
        if (d.ignore_arg) {
          return '<span class="layui-badge layui-bg-green">是</span>'
        } else {
          return '<span class="layui-badge layui-bg-orange">否</span>'
        }                
      }}
      ,{field: 'range', title: '分片回源',templet: function(d) {
        if (d.range) {
          return '<span class="layui-badge layui-bg-green">是</span>'
        } else {
          return '<span class="layui-badge layui-bg-orange">否</span>'
        }                
      }}
      ,{field: 'proxy_ignore_headers', title: '强制缓存',templet: function(d) {
        var proxy_ignore_headers_arr = d.proxy_ignore_headers.split(/\s+/)

        if ($.inArray("X-Accel-Expires",proxy_ignore_headers_arr) != -1 && $.inArray("Expires",proxy_ignore_headers_arr) != -1 && $.inArray("Cache-Control",proxy_ignore_headers_arr) != -1 && $.inArray("Set-Cookie",proxy_ignore_headers_arr) != -1) {
          return '<span class="layui-badge layui-bg-green">是</span>'
        } else {
          return '<span class="layui-badge layui-bg-orange">否</span>'
        }
      
      }}

      ,{field: 'no_cache', title: '不缓存条件',templet: function(d) {
          var str = []
          for (i in d.no_cache) {
            str.push(d.no_cache[i]['variable'] + " ~ " + d.no_cache[i]['string'])
          }
          return str.join(" or ")

      }}
      ,{fixed: 'right', title:'操作', toolbar: '#cache-bar', width:120}

    ]]
    ,data: []
    ,page: true
  }); 

  //缓存事件
  //头工具栏事件
  table.on('toolbar(cache)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id);
    var table_data = obj.config.data
    switch(obj.event){
      case 'add':
        admin.popup({
          title: '新增缓存'
          ,area: ['800px', '550px']
          ,id: 'LAY-popup-cache-add'
          ,success: function(layero, index){
            view(this.id).render('site/site/cache_form').done(function(){
              //监听提交
              form.on('submit(LAY-site-site-edit-cache-add-submit)', function(data){
                var field = data.field
                var type = field.cache_type; //获取提交的字段
                var content = field.cache_content
                var expire = field.cache_expire
                var unit = field.cache_unit
                var ignore_arg = $("input[name='cache_ignore_arg']").prop("checked")
                var cache_range = $("input[name='cache_range']").prop("checked")

                var no_cache = []
                $(".no-cache-item .del-span").each(function (index,ele) {
                  var item = $(ele).text()
                  var variable = item.split(" ~ ")[0]
                  var string = item.split(" ~ ")[1].slice(0,-1)
                  no_cache.push({"variable":variable,"string":string})
                })

                var proxy_ignore_headers_arr = []
                $("input[name='proxy_ignore_headers']:checked").each(function (){
                  proxy_ignore_headers_arr.push($(this).val())
                })

                var proxy_ignore_headers = proxy_ignore_headers_arr.join(" ")

                var proxy_cache = JSON.parse(JSON.stringify(table_data))
                for (i in proxy_cache) {
                  delete proxy_cache[i]['LAY_TABLE_INDEX']
                  delete proxy_cache[i]['LAY_CHECKED']
                }

                proxy_cache.push({"type":type,"content":content,"expire":expire,"unit":unit,"ignore_arg":ignore_arg,"range":cache_range, "proxy_ignore_headers":proxy_ignore_headers, "no_cache":no_cache})

                window.cache = proxy_cache
                table.reload("cache", {"data":proxy_cache})
                layer.close(index);


              });
            });
          }
        });
      break;
      case 'delete':
        var data = checkStatus.data;
        if (data.length == 0) {
          layer.alert('请选择需要删除的缓存配置');   
          return
        }

        layer.confirm('将删除所选的缓存配置，是否继续', function(index){
            var proxy_cache = []
            for (i in table_data) {
              if (!table_data[i]['LAY_CHECKED']) {
                proxy_cache.push(table_data[i])
              }
            }

            for (i in proxy_cache) {
              delete proxy_cache[i]['LAY_TABLE_INDEX']
              delete proxy_cache[i]['LAY_CHECKED']
            }

            window.cache = proxy_cache
            table.reload("cache", {"data":proxy_cache})
            layer.close(index);
        });
      break;                   
    };
  });
  
  //监听行工具事件
  table.on('tool(cache)', function(obj){
    var edit_index = $(obj.tr).data("index");
    var table_data = obj.data
    if(obj.event === 'edit'){
      admin.popup({
        title: '编辑缓存'
        ,area: ['800px', '550px']
        ,id: 'LAY-popup-cache-add'
        ,success: function(layero, index){
          view(this.id).render('site/site/cache_form',table_data).done(function(){
            $("#cache_type").val(table_data.type)
            $("#cache_unit").val(table_data.unit)
            $("input[name='cache_ignore_arg']").prop("checked",true)
            var proxy_ignore_headers_arr = table_data.proxy_ignore_headers.split(/\s+/)
            for (i in proxy_ignore_headers_arr) {
              $("input[name='proxy_ignore_headers'][value='"+proxy_ignore_headers_arr[i]+"']").prop("checked",true)
            }
            var no_cache = table_data.no_cache
            for (i in no_cache) {
              var variable = no_cache[i]['variable']
              var string = no_cache[i]['string']
              $(".no-data").remove()
              $(".no-cache").append('<div class="layui-form-mid no-cache-item"><span class="layui-badge layui-bg-blue del-span">'+variable+' ~ '+string+'<span onclick=\'del_item(this)\' class="layui-badge del">x</span></span></div>')                
            }

            form.render(null, 'layuiadmin-form-cacheadmin');
            
            //监听提交
            form.on('submit(LAY-site-site-edit-cache-add-submit)', function(data_submit){
              var field = data_submit.field
              var type = field.cache_type; //获取提交的字段
              var content = field.cache_content
              var expire = field.cache_expire
              var unit = field.cache_unit
              var ignore_arg = $("input[name='cache_ignore_arg']").prop("checked")
              var cache_range = $("input[name='cache_range']").prop("checked")

              var no_cache = []
              $(".no-cache-item .del-span").each(function (index,ele) {
                var item = $(ele).text()
                var variable = item.split(" ~ ")[0]
                var string = item.split(" ~ ")[1].slice(0,-1)
                no_cache.push({"variable":variable,"string":string})
              })

              var proxy_ignore_headers_arr = []
              $("input[name='proxy_ignore_headers']:checked").each(function (){
                proxy_ignore_headers_arr.push($(this).val())
              })

              var proxy_ignore_headers = proxy_ignore_headers_arr.join(" ")

              // 保存旧数据
              window.cache_pre = JSON.parse(JSON.stringify(window.cache))

              window.cache[edit_index] = {"type":type,"content":content,"expire":expire,"unit":unit,"range":cache_range, "ignore_arg":ignore_arg,"proxy_ignore_headers":proxy_ignore_headers, "no_cache":no_cache}
              var proxy_cache = JSON.parse(JSON.stringify(window.cache))

              table.reload("cache", {"data":proxy_cache})
              layer.close(index);

            });
          });
        }
      });
    }
  }); 

  url_ratelimit_table = []
  window.url_ratelimit = []
  table.render({
    elem: '#api-protect'
    ,title: 'xx'
    ,toolbar: '#api-protect-toolbar'
    ,autoSort: false
    ,defaultToolbar: []
      ,size: 'sm' //小尺寸的表格

    ,cols: [[ //表头
      {type: 'checkbox'}
      ,{field: 'uri', title: '匹配条件',templet:function(d) {
        var matcher = d.matcher
        var ret = ""
        for (item in matcher) {
          var op = matcher[item]["operator"]
          var value = matcher[item]["value"]
          ret += window.match_item_map[item] +" " + window.match_op_map[op] + " "  + value.split("\n").join(" 或 ") + " " + "<br>且<br>"
          
        }
        return ret.replace(/<br>且<br>$/,"")

      }}
      ,{field: 'within', title: '执行过滤',templet:function(d){
        var filter = d.filter
        var type = filter['type']
        var within_second = filter['within_second']
        var max_challenge = filter['max_challenge']
        var max_per_uri = filter['max_per_uri']
        if (type == "req_rate") {
          if (within_second == "11" && max_challenge == "11111" && max_per_uri=="11111") {
            return "放行"
          } else if (within_second == "11" && max_challenge == "0" && max_per_uri=="0") {
            return "拉黑"
          } else {
            return "针对单个IP地址:<br>总请求允许 " + max_challenge + "次/" + within_second + "秒<br>同URL允许 "+max_per_uri+"次/" + within_second + "秒"
          }
        } else {
          return window.filter_type_map[type]
        }

        
      }}

    ]]
    ,data: url_ratelimit_table
    ,page: false
            
  });   

  table.on('toolbar(api-protect)', function(obj){
      var checkStatus = table.checkStatus(obj.config.id);
      var table_data = obj.config.data
      switch(obj.event){
        case 'add':
          admin.popup({
            title: '新增自定义规则'
            ,area: ['750px', '550px']
            ,id: 'LAY-popup-api-protect-add'
            ,success: function(layero, index){
              view(this.id).render('site/site/url_ratelimit_form').done(function(){
                form.on('submit(LAY-site-site-edit-custom-rule-add-submit)', function(data){
                  var field = data.field
                  var uri = field.uri; //获取提交的字段
                  var within_second = field.within_second
                  var max_req = field.max_req
                  var max_req_per_uri = field.max_req_per_uri
                  var type = field["filter-type"]
                  if (type == "bypass" || type == "block") {
                    type = "req_rate"
                  }

                  if (type == undefined) {
                    layer.alert("请选择过滤")
                    return
                  }

                  if (within_second == "") {
                    layer.alert("请输入n秒内")
                    return
                  }

                  if (max_req == "") {
                    layer.alert("请输入最大次数")
                    return
                  }

                  if (max_req_per_uri == "" && type == "req_rate") {
                    layer.alert("请输入单URL最大次数")
                    return
                  }

                  // extra
                  var extra = {}
                  if (type == "url_auth") {
                    var key = field.url_auth_key
                    var sign_name = field.url_auth_sign_name
                    var sign_use_times = field.url_auth_sign_use_times 
                    var time_diff = field.url_auth_time_diff
                    var mode = field.url_auth_mode

                    if (key == "") {
                      layer.alert("请输入密钥")
                      return        
                    }

                    if (sign_name == "") {
                      layer.alert("请输入签名参数名")
                      return        
                    }

                    if (sign_use_times == "") {
                      layer.alert("请输入签名使用次数")
                      return        
                    }

                    if (time_diff == "") {
                      layer.alert("请输入最大时间相差")
                      return        
                    }

                    var time_name = field.url_auth_sign_name 
                    if (mode == "TypeA" && time_name == "" ) {
                      layer.alert("请输入时间戳参数名")
                      return              
                    }
                    
                    extra = {"key":key,"sign_name":sign_name,"time_name":time_name,"time_diff":time_diff,"sign_use_times":sign_use_times,"mode":mode}
                  };

                  // 检查匹配规则
                  if (match_rules.length == 0) {
                    layer.alert("请添加匹配条件")
                    return           
                  }

                  // 组合成类似的数据{"matcher":{"req_uri": {"operator": "AC", "value": ""}},
                  //                "filter":{"within_second": "60", "max_challenge": "5", "max_per_uri": "", "type": "req_rate", "extra": {}}}

                  var rule = {"matcher":{}}
                  for (let index = 0; index < match_rules.length; index++) {
                    const element = match_rules[index]
                    var item = element['item']
                    var op = element['op']
                    var value = element['value']
                    rule["matcher"][item] = {"operator":op, "value":value}
                    
                  }

                  rule["filter"] = {"within_second":within_second,"max_challenge":max_req,"max_per_uri":max_req_per_uri,"type": type,"extra":extra}
                  window.url_ratelimit.push(rule)

                  table.reload("api-protect",{
                    data: window.url_ratelimit
                    ,done:function() {
                      layer.close(index)
                    }
                  })


                });

              });
            }
          });
        break;
        case 'delete':
          var data = checkStatus.data;
          if (data.length == 0) {
            layer.alert('请选择需要删除的规则');   
            return
          }

          layer.confirm('将删除所选规则，是否继续', function(index){
              var url_ratelimit = []
              for (i in table_data) {
                if (!table_data[i]['LAY_CHECKED']) {
                  url_ratelimit.push(table_data[i])
                }
              }

              for (i in url_ratelimit) {
                delete url_ratelimit[i]['LAY_TABLE_INDEX']
                delete url_ratelimit[i]['LAY_CHECKED']
              }

              window.url_ratelimit = url_ratelimit
              layui.table.reload('api-protect',{"data":url_ratelimit}); //重载表格
              layer.close(index); //执行关闭 

          });
        break;                   
      };
    });  

    window.req_header = []
    req_header_table = []
    table.render({
      elem: '#req_header'
      ,title: 'xx'
      ,toolbar: '#req_header-toolbar'
      ,autoSort: false
      ,defaultToolbar: []
        ,size: 'sm' //小尺寸的表格

      ,cols: [[ //表头
        {type: 'checkbox', fixed: 'left'}
        ,{field: 'name', title: '名称'}
        ,{field: 'value', title: '值'}
        ,{fixed: 'right', title:'操作', toolbar: '#req_header-bar', width:120}

      ]]
      ,data: req_header_table
      ,page: true
    });

    //源站请求头事件
    //头工具栏事件
    table.on('toolbar(req_header)', function(obj){
      var checkStatus = table.checkStatus(obj.config.id);
      var table_data = obj.config.data
      switch(obj.event){
        case 'add':
          admin.popup({
            title: '新增源站请求头'
            ,area: ['500px', '320px']
            ,id: 'LAY-popup-req_header-add'
            ,success: function(layero, index){
              view(this.id).render('site/site/req_header_form').done(function(){
                //监听提交
                form.on('submit(LAY-site-site-edit-req_header-add-submit)', function(data){
                  var field = data.field
                  var name = field.req_header_name; //获取提交的字段
                  var value = field.req_header_value

                  var req_header = JSON.parse(JSON.stringify(table_data))
                  for (i in req_header) {
                    delete req_header[i]['LAY_TABLE_INDEX']
                    delete req_header[i]['LAY_CHECKED']
                  }

                  req_header.push({"name":name,"value":value})

                  window.req_header = req_header

                  layui.table.reload('req_header',{"data":req_header}); //重载表格
                  layer.close(index); //执行关闭 

                });
              });
            }
          });
        break;
        case 'delete':
          var data = checkStatus.data;
          if (data.length == 0) {
            layer.alert('请选择需要删除的请求头');   
            return
          }

          layer.confirm('将删除所选的请求头，是否继续', function(index){
              var req_header = []
              for (i in table_data) {
                if (!table_data[i]['LAY_CHECKED']) {
                  req_header.push(table_data[i])
                }
              }

              for (i in req_header) {
                delete req_header[i]['LAY_TABLE_INDEX']
                delete req_header[i]['LAY_CHECKED']
              }

              window.req_header = req_header
              layui.table.reload('req_header',{"data":req_header}); //重载表格
              layer.close(index); //执行关闭 

          });
        break;                   
      };
    });
    
    //监听行工具事件
    table.on('tool(req_header)', function(obj){
      var edit_index = $(obj.tr).data("index");
      var table_data = obj.data
      if(obj.event === 'edit'){
        admin.popup({
          title: '编辑请求头'
          ,area: ['500px', '320px']
          ,id: 'LAY-popup-req_header-add'
          ,success: function(layero, index){
            view(this.id).render('site/site/req_header_form',table_data).done(function(){
              form.render(null, 'layuiadmin-form-req_headeradmin');
              
              //监听提交
              form.on('submit(LAY-site-site-edit-req_header-add-submit)', function(data_submit){
                var field = data_submit.field
                var name = field.req_header_name; //获取提交的字段
                var value = field.req_header_value

                // 保存旧数据
                window.req_header_pre = JSON.parse(JSON.stringify(window.req_header))

                window.req_header[edit_index] = {"name":name,"value":value}
                var req_header = JSON.parse(JSON.stringify(window.req_header))

                layui.table.reload('req_header',{"data":req_header}); //重载表格
                layer.close(index); //执行关闭 

              });
            });
          }
        });
      }
    });  

    window.url_rewrite = []
    url_rewrite_table = []
    table.render({
      elem: '#url_rewrite'
      ,title: 'xx'
      ,toolbar: '#url_rewrite-toolbar'
      ,autoSort: false
      ,defaultToolbar: []
        ,size: 'sm' //小尺寸的表格

      ,cols: [[ //表头
        {type: 'checkbox', fixed: 'left'}
        ,{field: 'host', title: '域名端口'}
        ,{field: 'match', title: '匹配'}
        ,{field: 'redirect', title: '转向'}
        ,{field: 'code', title: '响应码'}
        ,{fixed: 'right', title:'操作', toolbar: '#url_rewrite-bar', width:120}

      ]]
      ,data: url_rewrite_table
      ,page: true
    });

    //源站请求头事件
    //头工具栏事件
    table.on('toolbar(url_rewrite)', function(obj){
      var checkStatus = table.checkStatus(obj.config.id);
      var table_data = obj.config.data
      switch(obj.event){
        case 'add':
          admin.popup({
            title: '新增URL转向'
            ,area: ['500px', '320px']
            ,id: 'LAY-popup-url_rewrite-add'
            ,success: function(layero, index){
              view(this.id).render('site/site/url_rewrite_form').done(function(){
                //监听提交
                form.on('submit(LAY-site-site-edit-url_rewrite-add-submit)', function(data){
                  var field = data.field
                  var host = field.url_rewrite_host;
                  var match = field.url_rewrite_match
                  var redirect = field.url_rewrite_redirect
                  var code = field.url_rewrite_code
                  var user_agent = field.url_rewrite_user_agent
                  var referer = field.url_rewrite_referer
                  var country_code = field.url_rewrite_country_code
                  var accept_language = field.url_rewrite_accept_language


                  var url_rewrite = JSON.parse(JSON.stringify(table_data))
                  for (i in url_rewrite) {
                    delete url_rewrite[i]['LAY_TABLE_INDEX']
                    delete url_rewrite[i]['LAY_CHECKED']
                  }

                  url_rewrite.push({"host":host,"match":match,"redirect":redirect,"code":code,"user_agent":user_agent,"referer":referer,"country_code":country_code,"accept_language":accept_language})

                  window.url_rewrite = url_rewrite

                  layui.table.reload('url_rewrite',{"data":url_rewrite}); //重载表格
                  layer.close(index); //执行关闭 

                });
              });
            }
          });
        break;
        case 'delete':
          var data = checkStatus.data;
          if (data.length == 0) {
            layer.alert('请选择需要删除的URL转向');   
            return
          }

          layer.confirm('将删除所选的URL转向，是否继续', function(index){
              var url_rewrite = []
              for (i in table_data) {
                if (!table_data[i]['LAY_CHECKED']) {
                  url_rewrite.push(table_data[i])
                }
              }

              for (i in url_rewrite) {
                delete url_rewrite[i]['LAY_TABLE_INDEX']
                delete url_rewrite[i]['LAY_CHECKED']
              }

              window.url_rewrite = url_rewrite
              layui.table.reload('url_rewrite',{"data":url_rewrite}); //重载表格
              layer.close(index); //执行关闭 

          });
        break;                   
      };
    });
    
    //监听行工具事件
    table.on('tool(url_rewrite)', function(obj){
      var edit_index = $(obj.tr).data("index");
      var table_data = obj.data
      if(obj.event === 'edit'){
        admin.popup({
          title: '编辑URL转向'
          ,area: ['500px', '320px']
          ,id: 'LAY-popup-url_rewrite-add'
          ,success: function(layero, index){
            view(this.id).render('site/site/url_rewrite_form',table_data).done(function(){
              form.render(null, 'layuiadmin-form-url_rewriteadmin');
              
              //监听提交
              form.on('submit(LAY-site-site-edit-url_rewrite-add-submit)', function(data_submit){
                var field = data_submit.field
                var host = field.url_rewrite_host;
                var match = field.url_rewrite_match
                var redirect = field.url_rewrite_redirect
                var code = field.url_rewrite_code
                var user_agent = field.url_rewrite_user_agent
                var referer = field.url_rewrite_referer
                var country_code = field.url_rewrite_country_code
                var accept_language = field.url_rewrite_accept_language


                // 保存旧数据
                window.url_rewrite_pre = JSON.parse(JSON.stringify(window.url_rewrite))

                window.url_rewrite[edit_index] = {"host":host,"match":match,"redirect":redirect,"code":code,"user_agent":user_agent,"referer":referer,"country_code":country_code,"accept_language":accept_language}
                var url_rewrite = JSON.parse(JSON.stringify(window.url_rewrite))

                layui.table.reload('url_rewrite',{"data":url_rewrite}); //重载表格
                layer.close(index); //执行关闭 

              });
            });
          }
        });
      }
    });  

    window.resp_header = []
    resp_header_table = []
    table.render({
      elem: '#resp_header'
      ,title: 'xx'
      ,toolbar: '#resp_header-toolbar'
      ,autoSort: false
      ,defaultToolbar: []
        ,size: 'sm' //小尺寸的表格

      ,cols: [[ //表头
        {type: 'checkbox', fixed: 'left'}
        ,{field: 'name', title: '名称'}
        ,{field: 'value', title: '值'}
        ,{fixed: 'right', title:'操作', toolbar: '#resp_header-bar', width:120}

      ]]
      ,data: resp_header_table
      ,page: true
    });

    //源站请求头事件
    //头工具栏事件
    table.on('toolbar(resp_header)', function(obj){
      var checkStatus = table.checkStatus(obj.config.id);
      var table_data = obj.config.data
      switch(obj.event){
        case 'add':
          admin.popup({
            title: '新增响应头'
            ,area: ['500px', '320px']
            ,id: 'LAY-popup-resp_header-add'
            ,success: function(layero, index){
              view(this.id).render('site/site/resp_header_form').done(function(){
                //监听提交
                form.on('submit(LAY-site-site-edit-resp_header-add-submit)', function(data){
                  var field = data.field
                  var name = field.resp_header_name; //获取提交的字段
                  var value = field.resp_header_value

                  var resp_header = JSON.parse(JSON.stringify(table_data))
                  for (i in resp_header) {
                    delete resp_header[i]['LAY_TABLE_INDEX']
                    delete resp_header[i]['LAY_CHECKED']
                  }

                  resp_header.push({"name":name,"value":value})

                  window.resp_header = resp_header

                  layui.table.reload('resp_header',{"data":resp_header}); //重载表格
                  layer.close(index); //执行关闭 

                });
              });
            }
          });
        break;
        case 'delete':
          var data = checkStatus.data;
          if (data.length == 0) {
            layer.alert('请选择需要删除的响应头');   
            return
          }

          layer.confirm('将删除所选的响应头，是否继续', function(index){
              var resp_header = []
              for (i in table_data) {
                if (!table_data[i]['LAY_CHECKED']) {
                  resp_header.push(table_data[i])
                }
              }

              for (i in resp_header) {
                delete resp_header[i]['LAY_TABLE_INDEX']
                delete resp_header[i]['LAY_CHECKED']
              }

              window.resp_header = resp_header
              layui.table.reload('resp_header',{"data":resp_header}); //重载表格
              layer.close(index); //执行关闭 

          });
        break;                   
      };
    });
    
    //监听行工具事件
    table.on('tool(resp_header)', function(obj){
      var edit_index = $(obj.tr).data("index");
      var table_data = obj.data
      if(obj.event === 'edit'){
        admin.popup({
          title: '编辑响应头'
          ,area: ['500px', '320px']
          ,id: 'LAY-popup-resp_header-add'
          ,success: function(layero, index){
            view(this.id).render('site/site/resp_header_form',table_data).done(function(){
              form.render(null, 'layuiadmin-form-resp_headeradmin');
              
              //监听提交
              form.on('submit(LAY-site-site-edit-resp_header-add-submit)', function(data_submit){
                var field = data_submit.field
                var name = field.resp_header_name; //获取提交的字段
                var value = field.resp_header_value

                // 保存旧数据
                window.resp_header_pre = JSON.parse(JSON.stringify(window.resp_header))

                window.resp_header[edit_index] = {"name":name,"value":value}
                var resp_header = JSON.parse(JSON.stringify(window.resp_header))

                layui.table.reload('resp_header',{"data":resp_header}); //重载表格
                layer.close(index); //执行关闭 

              });
            });
          }
        });
      }
    });  

  form.on('checkbox(active)', function(data){
    var is_checked = data.elem.checked
    var ele = data.elem
    var next = $(ele).parent().next()
    while (next.length) {
      next.find("input,select,textarea").attr("disabled",!is_checked)
      if (is_checked) {
        next.removeClass("layui-hide")
        table.resize("api-protect")
      } else {
        next.addClass("layui-hide")
      }
      next = next.next()
    }

    form.render()
    var id = $(ele).attr("id")
    if (id == "update_req_header_on") {
      table.resize("req_header")
    }

    if (id == "update_resp_header_on") {
      table.resize("resp_header")
    }

    if (id == "update_url_rewrite_on") {
      table.resize("url_rewrite")
    }

  });  

  function update_sites(req_data) {
    var size = req_data.length
    window.site_cancel = false

    var update_site = function (data, index, size) {
      admin.req({
        url: '/sites' //实际使用请改成服务端真实接口
        ,data: JSON.stringify(data[index])
        ,type: "put"
        ,loader: false
        ,contentType:"application/json"
        ,dataType: "json"
        ,done: function(res){
          element.progress('process', parseInt((index+1)/size*100)+"%");
          index += 1
          if (index == size) {
            layer.closeAll();
            layui.table.reload('test-table-toolbar'); //重载表格
          } else {
            if (!window.site_cancel) {
              update_site(data, index, size)
            }
          }
        }
      });
    }

    // 弹出进度条                    
    layer.open({
      title: '处理中...'
      ,content: '<div lay-filter="process" style="margin-left:20px;margin-right:20px;margin-top:20px;" class="layui-progress layui-progress-big" lay-showPercent="true"><div class="layui-progress-bar layui-bg-blue" lay-percent="0%"></div></div>'
      ,type: 1
      ,btn: ['取消']
      ,area: '300px'
      ,yes: function(index, layero){
        window.site_cancel = true
        layer.close(index);
      }
      ,cancel: function(){ 
        return false
      }
      ,success: function(layero, index){
        element.render()
        // 开始添加
        update_site(req_data, 0, size)
      }
    });

  }

  form.on('switch(update_http_on)', function(data){
    // 开启
    if (data.elem.checked) {
      $(".update_http_listen").removeClass("layui-hide")

    // 关闭
    } else {
      $(".update_http_listen").addClass("layui-hide")
    }

  });

  form.on('switch(update_https_on)', function(data){
    // 开启
    if (data.elem.checked) {
      $(".update_https_listen").removeClass("layui-hide")

    // 关闭
    } else {
      $(".update_https_listen").addClass("layui-hide")
    }

  });

  // 基本设置
  $("#update_base_btn").click(function () {
    var site_group_checked = $("#site-group-checked").prop("checked")
    var user_package_checked = $("#user-package-checked").prop("checked")

    // 网站分组
    var groups = undefined
    if (site_group_checked) {
      groups = $("select[name='update_site_group']").val()
      if (groups) {
        groups = groups.join(",")
      } else {
        groups = ""
      } 

    };

    // 套餐
    var user_package = undefined
    if (user_package_checked) {
      user_package = $("select[name='update_user_package']").val()
    }

    var req_data = []
    for (i in window.site_ids) {
      req_data.push({"id":window.site_ids[i],"groups":groups,"user_package":user_package})
    }

    update_sites(req_data)

  })

  // http设置
  $("#update_http_btn").click(function () {
    var is_checked = $("input[name='update_http_on']").prop("checked")
    var http_listen 
    if (is_checked) {
      var port_check = $("#http_listen_port_check").prop("checked")
      var protocol_check = $("#http_backend_protocol_check").prop("checked")
      var backend_port_check = $("#http_backend_port_check").prop("checked")

      // 监听端口
      var port = undefined
      if (port_check) {
        port = $("input[name='update_http_listen_port']").val()
      }

      http_listen = {"port":port}

    } else {
      http_listen = {}
    }

    var req_data = []
    for (i in window.site_ids) {
      req_data.push({"id":window.site_ids[i],"http_listen":http_listen})
    }

    update_sites(req_data)

  })

  // 监听ssl_mode
  form.on('radio(ssl_mode)', function(data){
    var ssl_mode = data.value
    if (ssl_mode == "old") {
      var ssl_protocols = "TLSv1 TLSv1.1 TLSv1.2 TLSv1.3"
      var ssl_ciphers = "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA"
      var ssl_prefer_server_ciphers = "on"
    } else if (ssl_mode == "intermediate") {
      var ssl_protocols = "TLSv1.2 TLSv1.3"
      var ssl_ciphers = "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384"
      var ssl_prefer_server_ciphers = "off"
    } else if (ssl_mode == "custom") {
      $(".ssl-config").removeClass("layui-hide")
    }

    if (ssl_mode == "old" || ssl_mode == "intermediate") {
      $(".ssl-config").addClass("layui-hide")
      $("input[name='https_listen_ssl_protocols']").val(ssl_protocols)
      $("input[name='https_listen_ssl_ciphers']").val(ssl_ciphers)
      $("input[name='https_listen_ssl_prefer_server_ciphers']").val(ssl_prefer_server_ciphers)        

    }

  }); 

  // https设置
  $("#update_https_btn").click(function () {
    var is_checked = $("input[name='update_https_on']").prop("checked")
    var https_listen 
    if (is_checked) {
      // 证书
      var https_cert_check = $("#https_cert_check").prop("checked")
      var cert = undefined
      if (https_cert_check) {
        cert = $("select[name='update_cert']").val() 
      }

      // 监听端口
      var https_listen_port_check = $("#https_listen_port_check").prop("checked")
      var port = undefined
      if (https_listen_port_check) {
        port = $("input[name='update_https_listen_port']").val()
      }

      // 回源协议
      var https_backend_protocol_check = $("#https_backend_protocol_check").prop("checked")
      var backend_protocol = undefined
      if (https_backend_protocol_check) {
        backend_protocol = $("input[name='update_https_backend_protocol']:checked").val()
      }

      // 回源端口
      var https_backend_port_check = $("#https_backend_port_check").prop("checked")
      var backend_port = undefined
      if (https_backend_port_check) {
        backend_port = $("input[name='update_https_backend_port']").val()
      }
      
      // HSTS
      var https_hsts_check = $("#https_hsts_check").prop("checked")
      var hsts = undefined
      if (https_hsts_check) {
        hsts = $("input[name='update_hsts']:checked").val()
        if (hsts) {
          hsts = parseInt(hsts)
        }        
      }

      // http2
      var https_http2_check = $("#https_http2_check").prop("checked")
      var http2 = undefined
      if (https_http2_check) {
        http2 = $("input[name='update_http2']:checked").val()
        if (http2) {
          http2 = parseInt(http2)
        }        
      }

      // ssl配置
      var https_ssl_config_check = $("#https_ssl_config_check").prop("checked")
      var ssl_protocols = undefined
      var ssl_ciphers = undefined
      var ssl_prefer_server_ciphers = undefined

      if (https_ssl_config_check) {
          ssl_protocols =  $("input[name='https_listen_ssl_protocols']").val()
          ssl_ciphers = $("input[name='https_listen_ssl_ciphers']").val()
          ssl_prefer_server_ciphers = $("input[name='https_listen_ssl_prefer_server_ciphers']").val()        
      }

      // 强制https
      var https_force_ssl_check = $("#https_force_ssl_check").prop("checked")
      var force_ssl_enable = undefined
      var force_ssl_port = undefined
      if (https_force_ssl_check) {
        force_ssl_enable = $("input[name='update_force_ssl_enable']:checked").val()
        if (force_ssl_enable) {
          force_ssl_enable = parseInt(force_ssl_enable)
        }        

        force_ssl_port = $("input[name='update_force_ssl_port']").val()

      }

      // 回源SSL协议
      var https_backend_ssl_protocol_check = $("#https_backend_ssl_protocol_check").prop("checked")
      var proxy_ssl_protocols = undefined
      if (https_backend_ssl_protocol_check) {
        proxy_ssl_protocols = $("input[name='update_proxy_ssl_protocols']").val()
      }

      // ocsp_stapling
      var https_ocsp_stapling_check = $("#https_ocsp_stapling_check").prop("checked")
      var ocsp_stapling = undefined
      if (https_ocsp_stapling_check) {
        ocsp_stapling = $("input[name='update_ocsp_stapling']:checked").val()
        if (ocsp_stapling) {
          ocsp_stapling = parseInt(ocsp_stapling)
        }        
      }


      https_listen = {"ssl_protocols":ssl_protocols,"ssl_ciphers":ssl_ciphers,"ssl_prefer_server_ciphers":ssl_prefer_server_ciphers,"ocsp_stapling":ocsp_stapling, "force_ssl_enable":force_ssl_enable,"force_ssl_port":force_ssl_port, "port":port,"cert":cert,"hsts":hsts,"http2":http2,"proxy_ssl_protocols":proxy_ssl_protocols}
    } else {
      https_listen = {}
    }

    var req_data = []
    for (i in window.site_ids) {
      req_data.push({"id":window.site_ids[i],"https_listen":https_listen})
    }

    update_sites(req_data)

  })

  // 源站设置
  $("#update_backend_btn").click(function () {
    // 回源协议
    var backend_protocol = undefined
    var backend_protocol_is_checked = $("#update_backend_protocol_on").prop("checked")
    if (backend_protocol_is_checked) {
      backend_protocol = $("#update_backend_protocol").val()
    }    

    // http回源端口
    var backend_http_port = undefined
    var backend_http_port_is_checked = $("#update_backend_http_port_on").prop("checked")
    if (backend_http_port_is_checked) {
      backend_http_port = $("input[name='update_backend_http_port']").val()
    }  

    // https回源端口
    var backend_https_port = undefined
    var backend_https_port_is_checked = $("#update_backend_https_port_on").prop("checked")
    if (backend_https_port_is_checked) {
      backend_https_port = $("input[name='update_backend_https_port']").val()
    }  

    // 回源超时
    var proxy_timeout = undefined
    var proxy_timeout_is_checked = $("#update_proxy_timeout_on").prop("checked")
    if (proxy_timeout_is_checked) {
      proxy_timeout = $("input[name='update_proxy_timeout']").val()
    }  

    // 负载方式
    var balance_way = undefined
    var balance_way_is_checked = $("#update_balance_on").prop("checked")
    if (balance_way_is_checked) {
      balance_way = $("#update_balance_way").val()
    }

    // 源站
    var update_backend_check = $("#update_backend_on").prop("checked")
    var backend = undefined
    if (update_backend_check) {
      backend = []
      var backend_ips = $("textarea[name='update_backend_ip']").val().split("\n")
      
      for (i in backend_ips) {
        if (backend_ips[i] == "") {
          continue
        }
        var state = "up"
        var ip_arr = backend_ips[i].split(/\s+/)
        ip = ip_arr[0]
        if (ip_arr.length == 2) {
          state = ip_arr[1]
        }
        backend.push({"addr": ip,"weight":1,"state":state})
      }

      if (backend.length == 0) {
        backend = undefined
      }      
    }

    // 回源端口映射
    var backend_port_mapping_check = $("#update_backend_port_mapping_on").prop("checked")
    var backend_port_mapping = undefined
    if (backend_port_mapping_check) {
      backend_port_mapping = $("input[name='update_backend_port_mapping']:checked").val()
      if (backend_port_mapping) {
        backend_port_mapping = parseInt(backend_port_mapping)
      }        
    }

    var req_data = []
    for (i in window.site_ids) {
      req_data.push({"id":window.site_ids[i], "proxy_timeout":proxy_timeout, "balance_way":balance_way,"backend":backend,"backend_protocol":backend_protocol,"backend_http_port":backend_http_port,"backend_https_port":backend_https_port,"backend_port_mapping":backend_port_mapping})
    }

    update_sites(req_data)

  })

  // 安全设置
  $("#update_cc_btn").click(function () {
    // cc rule
    var cc_default_rule_check = $("#cc_default_rule_check").prop("checked")
    var cc_default_rule = undefined
    if (cc_default_rule_check) {
      cc_default_rule = $("select[name='update_cc_default_rule']").val()
    }

    // 自动提升防御
    var auto_switch_check = $("#auto_switch_check").prop("checked")
    var cc_auto_switch = undefined
    if (auto_switch_check) {
      var switch_qps = $("input[name='update-switch-qps']").val()
      var enable = $("input[name='update_cc_switch_on']").prop("checked")
      var rule = $("#update_cc_switch_rule").val()
      if (enable) {
        cc_auto_switch = {"switch": switch_qps, "enable": enable, "rule": rule}
      } else {
        cc_auto_switch = {}

      }


    }

    // api防御
    var update_api_protect_on = $("#update_api_protect_on").prop("checked")
    var url_ratelimit = undefined
    if (update_api_protect_on) {
      url_ratelimit = window.url_ratelimit
    }

    // 屏蔽区域
    var update_block_region_on = $("#update_block_region_on").prop("checked")
    var block_region = undefined
    if (update_block_region_on) {
      block_region = $("select[name='update_block_region']").val()
    }

    // spider
    var spider_is_checked = $("#update_spider_on").prop("checked")
    var spider_allow = undefined
    if (spider_is_checked) {
      var spider = []
      $("input[name='update_spider_allow']:checked").each(function () {
        var name = $(this).val()
        spider.push(name)
      })

      spider_allow = spider.join("|")
    }


    // black_ip
    var black_ip = undefined
    var black_ip_is_checked = $("#update_black_ip_on").prop("checked")
    if (black_ip_is_checked) {
      black_ip = $("textarea[name='update_black_ip']").val()
    }

    // white_ip
    var white_ip = undefined
    var white_ip_is_checked = $("#update_white_ip_on").prop("checked")
    if (white_ip_is_checked) {
      white_ip = $("textarea[name='update_white_ip']").val()
    }

    // 屏蔽透明代理
    var block_proxy_check = $("#update_block_proxy_on").prop("checked")
    var block_proxy = undefined
    if (block_proxy_check) {
      block_proxy = $("input[name='update_block_proxy']:checked").val()
      if (block_proxy) {
        block_proxy = parseInt(block_proxy)
      }        
    }

    var req_data = []
    for (i in window.site_ids) {
      req_data.push({"id":window.site_ids[i],"block_proxy":block_proxy, "spider_allow":spider_allow,"black_ip":black_ip,"white_ip":white_ip,"cc_default_rule":cc_default_rule,"block_region":block_region,"cc_switch":cc_auto_switch,"extra_cc_rule":url_ratelimit })
    }

    update_sites(req_data)

  })


  // 访问控制
  $("#update_acl_btn").click(function () {

    // acl
    var acl_is_checked = $("#update_acl_on").prop("checked")
    var acl = undefined
    if (acl_is_checked) {
      acl = $("select[name='update_acl']").val()
      if (acl == "") {
        acl = null
      }
    }

    // hotlink
    var hotlink_is_checked = $("#update_hotlink_on").prop("checked")
    var hotlink = undefined
    if (hotlink_is_checked) {
      var enable = $("input[name='hotlink_enable']").prop("checked")
      var scope_type = $("input[name='hotlink_scope_type']:checked").val()
      var scope_content = $("input[name='hotlink_scope_content']").val()
      var allow_empty = $("input[name='hotlink_allow_empty']").prop("checked")
      var domain = $("input[name='hotlink_domain']").val()
      hotlink = {"scope_type": scope_type, "domain": domain, "enable": enable, "allow_empty": allow_empty, "scope_content": scope_content}
      
    }


    var req_data = []
    for (i in window.site_ids) {
      req_data.push({"id":window.site_ids[i],"acl":acl,"hotlink":hotlink})
    }

    update_sites(req_data)

  })

  // 监听回源host radio
  form.on('radio(update_backend_host_type)', function(data){
    var backend_host_type = data.value
    if (data.value == "$host") {
      $(".update_backend_host").addClass("layui-hide")

    } else {
      $(".update_backend_host").removeClass("layui-hide")
    }
    
  }); 

  // 回源设置
  $("#update_backend_host_btn").click(function () {
    // backend_host
    var backend_host_is_checked = $("#update_backend_host_on").prop("checked")
    var backend_host = undefined
    if (backend_host_is_checked) {
      var default_radio_checked = $("input[name='update_backend_host_type']").prop("checked")
      if (default_radio_checked) {
        backend_host = "$host"
      } else {
        backend_host = $("input[name='update_backend_host']").val()
      }
    }

    // range
    var range_is_checked = $("#update_range_on").prop("checked")
    var range = undefined
    if (range_is_checked) {
      range = $("input[name='update_range']").prop("checked")
    }

    var req_data = []
    for (i in window.site_ids) {
      req_data.push({"id":window.site_ids[i],"backend_host":backend_host,"range":range})
    }

    update_sites(req_data)

  })

  // 高级配置
  $("#update_advance_btn").click(function () {
    // gzip_enable
    var gzip_enable_is_checked = $("#update_gzip_on").prop("checked")
    var gzip_enable = undefined
    if (gzip_enable_is_checked) {
      gzip_enable = $("input[name='update_gzip_enable']").prop("checked")
    }

    // gzip_types
    var gzip_types_is_checked = $("#update_gzip_types_on").prop("checked")
    var gzip_types = undefined
    if (gzip_types_is_checked) {
      gzip_types = $("input[name='update_gzip_types']").val()
    }

    // websocket
    var websocket_is_checked = $("#update_websocket_on").prop("checked")
    var websocket = undefined
    if (websocket_is_checked) {
      websocket = $("input[name='update_websocket']").prop("checked")
    }

    // page_404
    var page_404_is_checked = $("#update_404_on").prop("checked")
    var page_404 = undefined
    if (page_404_is_checked) {
      page_404 = $("textarea[name='update_page_404']").val()
    }

    // page_50x
    var page_50x_is_checked = $("#update_50x_on").prop("checked")
    var page_50x = undefined
    if (page_50x_is_checked) {
      page_50x = $("textarea[name='update_page_50x']").val()
    }

    // req_header
    var req_header_enable_is_checked = $("#update_req_header_on").prop("checked")
    var req_header_enable = undefined
    var req_header = undefined
    if (req_header_enable_is_checked) {
      req_header = window.req_header
    }

    // resp_header
    var resp_header_enable_is_checked = $("#update_resp_header_on").prop("checked")
    var resp_header_enable = undefined
    var resp_header = undefined
    if (resp_header_enable_is_checked) {
      resp_header = window.resp_header
    }

    // url_rewrite
    var url_rewrite_enable_is_checked = $("#update_url_rewrite_on").prop("checked")
    var url_rewrite_enable = undefined
    var url_rewrite = undefined
    if (url_rewrite_enable_is_checked) {
      url_rewrite = window.url_rewrite
    }


    var req_data = []
    for (i in window.site_ids) {
      req_data.push({"id":window.site_ids[i],"gzip_enable":gzip_enable,"gzip_types":gzip_types,"websocket_enable":websocket,"page_404":page_404,"page_50x":page_50x,"req_header":req_header,"resp_header":resp_header,"url_rewrite":url_rewrite})
    }

    update_sites(req_data)

  })
  
  // 缓存配置
  $("#update_proxy_cache_btn").click(function (argument) {
    var req_data = []
    for (i in window.site_ids) {
      req_data.push({"id":window.site_ids[i],"proxy_cache":window.cache})
    }

    update_sites(req_data)

  })

});
</script>